package org.spring.springboot.controller;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.service.UserMenuService;
import org.spring.springboot.util.MessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 描述：用户菜单关联Controller
 * <p>
 * Author: 赵新国
 * Date: 2017/10/12 17:53
 */
@RestController
@RequestMapping("/admin")
public class UserMenuController {

    @Autowired
    private UserMenuService userMenuService;

    /**
     * 获取某用户所拥有菜单
     *
     * @return
     */
    @GetMapping(value = "getUserMenuIds")
    public List<Short> getUserMenuIds(@RequestParam Short userId) {
        return userMenuService.getUserMenuIds(userId);
    }

    /**
     * 保存用户菜单权限
     * @param userId
     * @return
     */
    @PostMapping(value = "saveUserMenu")
    public HashMap<String, Object> saveUserMenu(@RequestParam Short userId, HttpServletRequest request) {
        HashMap<String, Object> retMap = MessageUtils.success();
        try {
            String menuIdListString = request.getParameter("menuIdList");
            List<Short> menuIdList = new ArrayList<Short>();
            if (StringUtils.isNotBlank(menuIdListString)) {
                menuIdList = JSON.parseArray(menuIdListString, Short.class);
            }
            userMenuService.saveUserMenu(userId, menuIdList);
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

}
